package entity;

import cn.hutool.Hutool;
import cn.hutool.core.util.RandomUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.List;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Book {

    private Integer id;

    private String name;

    private String author;

    private String publish;

    private Integer sort;




    /*随机生成一本书的静态方法*/
    public static Book randomBook(){
        String name = RandomUtil.randomString(20);
        String author = RandomUtil.randomString(10);
        String publish = RandomUtil.randomString(20);
        Integer sort = RandomUtil.randomInt(1,500000);

        return new Book(null,name,author,publish,sort);

    }

    /*随机生成booklist集合*/    //size 生成的书的数量
    public static List<Book> randomBookList(Integer size){
        ArrayList<Book> books = new ArrayList<>();
        for (int i = 0;i<size;i++) {
            books.add(randomBook());
        }
        return books;
    }
    /*List对象------>CSV格式*/
    public static List<String> listToCSVString(Integer size){
        ArrayList<String> books = new ArrayList<>();

        for (Integer i = 0; i < size; i++) {
            books.add(randomBook().bookTocsvString());
        }
        return books;

    }
    //CSV格式转换为-----Book对象
    public static Book CsvToBook(String s){
        String[] split = s.split(",");
        String name = split[0];
        String author = split[1];
        String publish = split[2];
        Integer sort = Integer.parseInt(split[3]);
        return new Book(null,name,author,publish,sort);


    }

    //Book---->CSV格式
    public String bookTocsvString(){
        return  this.name+","+this.author+","+this.publish+","+this.sort;

    }

    public static void main(String[] args) {
        List<Book> books = randomBookList(10);
        for (Book book : books) {
            System.out.println(book.bookTocsvString());
        }
    }
}
